using EbusinessWebSite.Models;
using Grpc.Core;
using Microsoft.AspNetCore.Mvc;
using ProductService.Services;
using System.Diagnostics;

namespace EbusinessWebSite.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IProductService.IProductServiceClient _productClient;

    

        public HomeController(ILogger<HomeController> logger, IProductService.IProductServiceClient productClient)
        {
            _logger = logger;
            _productClient = productClient;
        }
        [HttpGet("{id}")]
        public async Task<ActionResult<ProductDto>> GetProduct(int id)
        {
            try
            {
                var request = new ProductFrom { Id = id };
                var product = await _productClient.GetProductAsync(request);
                return Ok(product);
            }
            catch (RpcException ex) when (ex.StatusCode == Grpc.Core.StatusCode.NotFound)
            {
                return NotFound();
            }
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
